package com.softtown.modules.dapai.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompMacDao;
import com.softtown.modules.dapai.dto.CompMacAddDTO;
import com.softtown.modules.dapai.dto.CompMacDTO;
import com.softtown.modules.dapai.dto.CompMacDetailDTO;
import com.softtown.modules.dapai.entity.CompMacScrewEntity;
import com.softtown.modules.dapai.entity.CompMacEntity;
import com.softtown.modules.dapai.service.CompMacDetailService;
import com.softtown.modules.dapai.service.CompMacService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: Ding Ya Yong
* @Description: 设备
* @date: 2022/1/2 11:38
* @Param:
* @return:
*/

@Service
public class CompMacServiceImpl extends
        BaseServiceImpl<CompMacDao, CompMacEntity> implements CompMacService {


    @Resource
    private CompMacDetailService compMacDetailService;


    @Override
    public PageInfo<CompMacDTO> getMacPageList(CompMacDTO compMacDTO) {
        Integer pageSize = compMacDTO.getPageSize();
        Integer page = compMacDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompMacDTO> compMacDTOList =
                baseDao.getMacPageList(compMacDTO.getCompId(), compMacDTO.getParam());
        return  new PageInfo<>(compMacDTOList);
    }

    @Override
    public List<CompMacDTO> listForMac(CompMacDTO compMacDTO) {
//        Integer pageSize = compMacDTO.getPageSize();
//        Integer page = compMacDTO.getPage();
//        if (pageSize > 0 && page > 0) {
//            PageHelper.startPage(page, pageSize);
//        }
        List<CompMacDTO> compMacDTOList = baseDao.listForMac(compMacDTO.getCompId(), compMacDTO.getParam(), compMacDTO.getMacTypeId());
        return  compMacDTOList;
    }

    @Override
    public CompMacAddDTO getMacById(Long compId, Long id) {
        CompMacAddDTO compMacAddDTO = new CompMacAddDTO();
        compMacAddDTO.setCompMacEntity(baseDao.getMacById(compId, id));
//        compMacAddDTO.setCompMacScrewEntityList(baseDao.getMacDetailList(compId, id));
        return compMacAddDTO;
    }

    @Override
    @Transactional
    public void addMac(CompMacAddDTO CompMacAddDTO) {
        CompMacEntity compMacEntity =ConvertUtils
                .sourceToTarget(CompMacAddDTO.getCompMacEntity(), CompMacEntity.class);
        insert(compMacEntity);
//        CompMacAddDTO.getCompMacScrewEntityList().forEach(item ->{
//            item.setMacId(compMacEntity.getId());
//            item.setCompId(compMacEntity.getCompId());
//        });
//        compMacDetailService.insertBatch(ConvertUtils
//                .sourceToTarget(CompMacAddDTO.getCompMacScrewEntityList(), CompMacScrewEntity.class));

    }

    @Override
    @Transactional
    public void updateMac(CompMacAddDTO CompMacAddDTO) {
        CompMacEntity compMacEntity =ConvertUtils
                .sourceToTarget(CompMacAddDTO.getCompMacEntity(), CompMacEntity.class);
        updateById(compMacEntity);
//        baseDao.deleteDetail(compMacEntity.getCompId(), compMacEntity.getId());
//        CompMacAddDTO.getCompMacScrewEntityList().forEach(item ->{
//            item.setMacId(compMacEntity.getId());
//            item.setCompId(compMacEntity.getCompId());
//        });
//        compMacDetailService.insertBatch(ConvertUtils
//                .sourceToTarget(CompMacAddDTO.getCompMacScrewEntityList(), CompMacScrewEntity.class));
    }

    @Override
    public void deleteMac(CompMacDTO compMacDTO) {
        baseDao.deleteMac(compMacDTO.getCompId(), compMacDTO.getId());
//        baseDao.deleteDetail(compMacDTO.getCompId(), compMacDTO.getId());
    }
}
